// 129.求根节点到叶节点数字之和
package Leetcode_Second_Brush;

public class Solution129 {
    private int result = 0;

    public int sumNumbers(TreeNode root) {
        DFS(root, 0);
        return result;
    }

    private void DFS(TreeNode root, int val) {
        if (root == null) {
            return;
        } else if (root.left == null && root.right == null) {
            result += val * 10 + root.val;
        } else {
            if (root.left != null) {
                DFS(root.left, val * 10 + root.val);
            }
            if (root.right != null) {
                DFS(root.right, val * 10 + root.val);
            }
        }
    }
}
